import {BrowserType, ConfigObj, PageState, UrlOpenType} from "@/utils/constants";
import {Bookmark, loadBookmarksJSON} from "@/utils/bookmarkUtils";

/**
 * 判断obj是否有属性
 * @param obj
 * @param prop
 */
function hasProperty(obj: Object, prop: string): boolean {
    return Object.keys(obj).includes(prop);
}


/**
 * 获取浏览器类型
 */
function getBrowserType(): BrowserType {
    const userAgent: string = navigator.userAgent;
    console.log("userAgent", userAgent);
    let browserType: BrowserType;
    switch (true) {
        case userAgent.includes('Chrome'):
            browserType = BrowserType.chrome;
            break;
        case userAgent.includes('Firefox'):
            browserType = BrowserType.firefox;
            break;
        case userAgent.includes('Safari'):
            browserType = BrowserType.safari;
            break;
        case userAgent.includes('Edge'):
            browserType = BrowserType.edge;
            break;
        case userAgent.includes('QQBrowser'):
            browserType = BrowserType.qqbrowser;
            break;
        case userAgent.includes('Chromium'):
            browserType = BrowserType.chromium;
            break;
        case userAgent.includes('360Browser'):
            browserType = BrowserType.browser360;
            break;
        default:
            browserType = BrowserType.chrome;
    }
    return browserType;
}


/**
 * 获取链接打开方式的名字
 * @param value
 */
function getUrlOpenTypeKey(value: UrlOpenType): string {
    const keys = Object.keys(UrlOpenType).filter((key) => UrlOpenType[key as keyof typeof UrlOpenType] === value);
    return keys.length > 0? keys[0] : "";
}


/**
 * 获取链接打开方式的值
 * @param key
 */
function getUrlOpenTypeValue(key: string): string | undefined {
    return UrlOpenType[key as keyof typeof UrlOpenType];
}


/**
 * 数组删除元素
 * @param array
 * @param item
 */
function arrayRemoveItem(array: any[], item: any) {
    const index = array.indexOf(item);
    if (index !== -1) {
        array.splice(index, 1);
    }
}


/**
 * 解析页面状态中的书签
 * @param pageState
 */
function parsePageStateBookmark(pageState: PageState) {
    if (!pageState || !pageState.bookmark) {
        return [];
    }
    let bookmarks: Bookmark[] = [];
    if (typeof pageState.bookmark == "string") {
        let bookmarksJSON = pageState.bookmark;
        bookmarks = loadBookmarksJSON(bookmarksJSON);
    }
    else {
        bookmarks = pageState.bookmark;
    }
    // 如果没有书签就不备份了
    if (!bookmarks) {
        return [];
    }
    pageState.bookmark = bookmarks;
    return bookmarks;
}


function overwriteConfig(mainConfigObj: ConfigObj, otherConfigObj: ConfigObj) {
    mainConfigObj.browserType = getBrowserType();
    mainConfigObj.isCollapsedWhenBookmarkTreeNullSearch = otherConfigObj.isCollapsedWhenBookmarkTreeNullSearch;
    mainConfigObj.language = otherConfigObj.language;
    mainConfigObj.isShowAffix = otherConfigObj.isShowAffix;
    mainConfigObj.urlOpenType = otherConfigObj.urlOpenType;
    mainConfigObj.importBookmarkType = otherConfigObj.importBookmarkType;
    mainConfigObj.isStoreBookmarkExpandStatus = otherConfigObj.isStoreBookmarkExpandStatus;
    mainConfigObj.watchBookmarkChange = otherConfigObj.watchBookmarkChange;
    if (mainConfigObj.syncServiceSettingMap) {
        for (let key in otherConfigObj.syncServiceSettingMap) {
            mainConfigObj.syncServiceSettingMap[key].token = otherConfigObj.syncServiceSettingMap[key].token;
            mainConfigObj.syncServiceSettingMap[key].syncMethod = otherConfigObj.syncServiceSettingMap[key].syncMethod;
            mainConfigObj.syncServiceSettingMap[key].cronSyncTime = otherConfigObj.syncServiceSettingMap[key].cronSyncTime;
            mainConfigObj.syncServiceSettingMap[key].enable = otherConfigObj.syncServiceSettingMap[key].enable;
        }
    }
    else {
        mainConfigObj.syncServiceSettingMap = otherConfigObj.syncServiceSettingMap;
    }
}


export {hasProperty, getBrowserType, getUrlOpenTypeKey, getUrlOpenTypeValue, arrayRemoveItem, parsePageStateBookmark, overwriteConfig};